package com.wistinga.timer;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Chronometer.OnChronometerTickListener;

public class Timer extends Activity implements OnClickListener, OnChronometerTickListener {
	
	private static final String TAG = "Timer"; 
	
	private boolean is_running;
	private int mTime;
	private Chronometer chrono;
	private NumberPicker np_minutes, np_seconds;
	private TextView tv_clock;
	private Button bt_StartStop;
	private LinearLayout ll_set_time;
	private MediaPlayer media_player;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        is_running = false;

        setVolumeControlStream(AudioManager.STREAM_ALARM);
        setContentView(R.layout.main);
        
        InitWidgets();
        
        /*
        AudioManager audio_manager = (AudioManager) getSystemService(AUDIO_SERVICE);
        int vol = audio_manager.getStreamVolume(AudioManager.STREAM_ALARM);
        bt_StartStop.setText(String.format("Volume: %d/%d", vol, audio_manager.getStreamMaxVolume(AudioManager.STREAM_ALARM)));
		*/

        try {
        	media_player = new MediaPlayer();
            AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.alarm_beep);

            media_player.setDataSource(afd.getFileDescriptor(),
                             afd.getStartOffset(),
                             afd.getLength());

            if (media_player != null) {
            	media_player.setAudioStreamType(AudioManager.STREAM_ALARM);
            	media_player.setLooping(true);
            	media_player.prepare();
            }
        } catch (Exception ex) {
            Log.w(TAG, "Couldn't create alarm sound", ex);
        }

    }
    
    private void InitWidgets() {
        tv_clock = (TextView) findViewById(R.id.tv_clock);
        
        ll_set_time = (LinearLayout) findViewById(R.id.ll_set_time);
        
        chrono = (Chronometer) findViewById(R.id.chrono);
        chrono.setOnChronometerTickListener(this);

        np_minutes = (NumberPicker) findViewById(R.id.np_minutes);
        np_minutes.setRange(0, 99);
        np_minutes.setSpeed(100);
        
        np_seconds = (NumberPicker) findViewById(R.id.np_seconds);
        np_seconds.setRange(0, 59);
        np_seconds.setSpeed(100);
        np_seconds.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
        
        bt_StartStop = (Button) findViewById(R.id.bt_start_stop);
        bt_StartStop.setOnClickListener(this);    	
    }

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.bt_start_stop:
			if (is_running) {
				chrono.stop();
				bt_StartStop.setText(R.string.start);
				tv_clock.setVisibility(View.GONE);
				ll_set_time.setVisibility(View.VISIBLE);
				is_running = false;
			}
			else {				
				mTime = np_minutes.getCurrent() * 60 + np_seconds.getCurrent();
				if (mTime > 0) {
					chrono.start();
					bt_StartStop.setText(R.string.stop);
					ll_set_time.setVisibility(View.GONE);
					tv_clock.setVisibility(View.VISIBLE);
					is_running = true;
				}
			}
			break;
		}
	}
	
	private String FormatTime(int time) {
		return String.format("%d:%02d", time / 60, time % 60);	
	}


	@Override
	public void onChronometerTick(Chronometer chronometer) {
		tv_clock.setText(FormatTime(mTime));
		if (mTime == 0) {
			onClick(bt_StartStop);
			media_player.start();
		}
		mTime--;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (media_player != null) {
			media_player.release();
			media_player = null;
		}
	}
}